import { _decorator, Color, Component, Material, MeshRenderer, ModelComponent, Node, TiledUserNodeData, tween, Vec3 } from 'cc';
import SoundManager from './SoundManager';
const { ccclass, property } = _decorator;

@ccclass('Ele')
export class Ele extends Component {
    @property(Node)
    private mTourn=null;
    private mData;
    protected onLoad(): void {
        
    }

    public setData(data){
        this.mData=data;
    }

    public isSame(node:Node){
        return this.mData==node.getComponent(Ele).data;
    }

    public addMat(mat:Material){
        let tmr:MeshRenderer=this.node.getComponent(MeshRenderer);
        if(!tmr.getSharedMaterial(0))tmr.setSharedMaterial(mat,0);
    }

    private mMovePos:Vec3=new Vec3();
    public updatePos(x,y,z,tw:boolean=false,time=0.2){
        this.mMovePos.set(x,y,z);
        if(!tw)this.node.setPosition(x,y,z);
        else {
            if(this.node.position.y!=y){
                let tisDown = this.node.position.y>y;
                let tsound = tisDown?"NutUp":"NutTurn";
                SoundManager.ins.playOneShot(tsound);
                tween(this.node).to(time,{position:this.mMovePos}).call(()=>{
                    if(tisDown)SoundManager.ins.playOneShot("down");
                }).start();
            }
            
        }
    }

    public setEleColor(c:Color){
        if(!c)return;
        let tmr:MeshRenderer=this.node.getComponent(MeshRenderer);
        let tmat:Material = tmr.material
        if(tmat)tmat.setProperty('albedo', c); 
    }

    public setSkinTexture(texture){
        if(!texture)return;
        let tmr:MeshRenderer=this.mTourn.getComponent(MeshRenderer);
        let tmat:Material = tmr.material;
        if(tmat){
            tmat.setProperty('mainTexture', texture);
        }
    }

    public setSkinTextureAndId(texture,id){
        if(!texture)return;
        let tmr:MeshRenderer=this.mTourn.getComponent(MeshRenderer);
        let tmat:Material = tmr.material;
        if(tmat){
            tmat.setProperty('mainTexture', texture);
        }
        this.setData(id);
    }

    public setSkinTextureInSameId(texture,id){
        if(id==this.getChangeId()){
            this.setSkinTexture(texture);
            this.change();
            return true;
        }
        return false;
    }

    public getIsSpId(){
        return this.mData>10;
    }
   public getChangeId(){
      if(this.mData<10)return -1;  
      let tid = this.mData%10;
      return tid;
   }

   public change(){
        let tid = this.getChangeId()
        if(tid>0)this.setData(tid);
   }


    public get data(){
        return this.mData;
    }
}


